using System;

namespace SharpMud.Server.Systems.Security.Entities
{
	public class EntityReference
	{
		#region Private Variables
		private System.Guid	_ID;
		private string		_Name;
		private IEntity		_Entity;
		#endregion

		#region Constructors
		public EntityReference()
		{
			_Name		= String.Empty;
			_ID			= Guid.Empty;
			_Entity		= null;
		}

		public EntityReference(IEntity entity)
		{
			_Name		= entity.Name;
			_ID			= entity.ID;
			_Entity		= entity;
		}

		public EntityReference(string entityName)
		{
			_Name		= entityName;
			_ID			= Guid.Empty;
			_Entity		= null;		
		}

		public EntityReference(string entityName, System.Guid entityID)
		{
			_Name		= entityName;
			_ID			= entityID;
			_Entity		= null;
		}
		#endregion

		#region Overloads
		public override bool Equals(object obj)
		{
			if(obj.GetType().IsSubclassOf(typeof(EntityReference)) || obj.GetType()==typeof(EntityReference))
			{
				return Equals((EntityReference)obj);
			}
			else if(obj.GetType().GetInterface(typeof(IEntity).FullName,true)!=null)
			{
				return Equals(new EntityReference((IEntity)obj));
			}
			else
			{
				return base.Equals(obj);
			}
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		#region op_Equals
		public static bool operator==(EntityReference obj1, EntityReference obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(EntityReference obj1, object obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(object obj1, EntityReference obj2)
		{
			return(obj2.Equals(obj1));
		}

		public static bool operator==(EntityReference obj1, IEntity obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(IEntity obj1, EntityReference obj2)
		{
			return(obj2.Equals(obj1));
		}
		#endregion
		#region op_NotEquals
		public static bool operator!=(EntityReference obj1, EntityReference obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(EntityReference obj1, object obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(object obj1, EntityReference obj2)
		{
			return(!obj2.Equals(obj1));
		}

		public static bool operator!=(EntityReference obj1, IEntity obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(IEntity obj1, EntityReference obj2)
		{
			return(!obj2.Equals(obj1));
		}
		#endregion
		public bool Equals(EntityReference obj)
		{
			if(
				this._Entity==null && this._ID==Guid.Empty && this._Name==String.Empty
				&&
				obj._Entity==null && obj._ID==Guid.Empty && obj._Name==String.Empty
				)
			{
				return true;
			}

			if((this._Entity==obj._Entity) && obj._Entity!=null)
				return true;
			if((this._Entity!=obj._Entity) 
				&& (this._Entity!=null) && (obj._Entity!=null))
				return false;
			
			if((this._ID==obj._ID) && obj._ID!=Guid.Empty)
				return true;
			if((this._ID!=obj._ID) 
				&& (this._ID!=Guid.Empty) && (obj._ID!=Guid.Empty))
				return false;

            if((this._Name==obj._Name) && obj._Name!=String.Empty)
				return true;
			if((this._Name!=obj._Name) 
				&& (this._Name!=String.Empty) && (obj._Name!=String.Empty))
				return false;

			return false;
		}

		#endregion

		public string Name
		{
			get
			{
				return this._Name;
			}
		}

		public System.Guid ID
		{
			get
			{
				return this._ID;
			}
		}

		public IEntity Entity
		{
			get
			{
				return this._Entity;
			}
		}
		
		#region Static Properties
		public static EntityReference Empty
		{
			get
			{
				return new EntityReference();
			}
		}
		#endregion
	}
}